﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Prop;

namespace Web.pages
{
    public partial class loginreport : System.Web.UI.Page
    {

        private Int32 idx = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            grvResult.RowDataBound += GrvResultOnRowDataBound;
            grvResult.PageIndexChanging += GrvResultOnPageIndexChanging;
            if (!IsPostBack)
            {
                OnReload();
            }
        }

        private void GrvResultOnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvResult.PageIndex = e.NewPageIndex;
            OnReload();
        }

        private void GrvResultOnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;

            Label lbNo = (Label)e.Row.FindControl("lbNo");

            if (lbNo != null)
                lbNo.Text = ((idx + 1) + (grvResult.PageIndex * grvResult.PageSize)).ToString();

            idx += 1;
        }

        private void OnReload()
        {
            try
            {
                var o = Dao.UserlogDao.Init().ReportLoginSelect();
                if (o.Count > 0)
                {
                    grvResult.DataSource = o.ToList();
                    grvResult.DataBind();
                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = ((USER)Session["UserInfo"]).UID
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }
            upl.Update();
        }    
    }
}